//
//  GQTabBarItem.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/8.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQTabBarItem.h"

@implementation GQTabBarItem

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:11];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return self;
}

- (void)setItem:(UITabBarItem *)item
{
    _item = item;

    [self setTitle:_item.title forState:UIControlStateNormal];
    [self setImage:_item.image forState:UIControlStateNormal];
    [self setTitleColor:[UIColor colorWithRed:84/255.0 green:124/255.0 blue:152/255.0 alpha:1.0] forState:UIControlStateSelected];
    [self setImage:_item.selectedImage forState:UIControlStateSelected];
//    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
//    
//    // KVO：时刻监听一个对象的属性有没有改变
//    // 给谁添加观察者
//    // Observer:按钮
//    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
//    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
//    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];

}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    [self setTitle:_item.title forState:UIControlStateNormal];
//    [self setImage:_item.image forState:UIControlStateNormal];
//    [self setImage:_item.selectedImage forState:UIControlStateSelected];
//}

- (void)setHighlighted:(BOOL)highlighted
{
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    CGFloat imageViewW = height * 0.6;
    CGFloat imageViewH = imageViewW;
    CGFloat imageViewX =  (width - imageViewW) * 0.5;
    CGFloat imageViewY = imageViewH * 0.1;
    
    self.imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
    CGFloat titleLabelW = width;
    CGFloat titleLabelH = height * 0.3;
    CGFloat titleLabelX = 0;
    CGFloat titleLabelY = height * 0.7;
    self.titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    
}

@end
